/*
 * Menu.cpp
 *
 *  Created on: 2010-01-09
 *      Author: morek
 */

#include "Menu.h"

using namespace cv;
char* intToStr(int n) {
	int i = 0;
	char *tmp = (char*) malloc(12);
	char *ret = (char*) malloc(12);
	if (n < 0) {
		*ret = '-';
		i++;
		n = -n;
	}
	do {
		*tmp = n % 10 + 48;
		n -= n % 10;
		if (n > 9)
			*tmp++;
	} while (n /= 10);
	while ((ret[i++] = *tmp--))
		;
	return ret;
}
int menu(CvArr* image, Fizyka pilka, int width, int height) {
	CvFont czcionka;
	int dzielnik = 5;
	CvScalar kolor1 = CV_RGB(60,60,60);
	CvScalar kolor2 = CV_RGB(20,160,0);
	CvPoint pts[6];
	int x = width / 16;
	int y = 0;
	if (pilka.y > (height / dzielnik) * 4 - 55)
		y = (height / dzielnik) * 4 - 50;
	else if (pilka.y > (height / dzielnik) * 3 - 55)
		y = (height / dzielnik) * 3 - 50;
	else if (pilka.y > (height / dzielnik) * 2 - 55)
		y = (height / dzielnik) * 2 - 50;
	else
		y = (height / dzielnik) * 1 - 50;

	pts[0] = cvPoint(x * 2, y + 40);
	pts[1] = cvPoint(x * 4, y);
	pts[2] = cvPoint(x * 12, y);
	pts[3] = cvPoint(x * 14, y + 40);
	pts[4] = cvPoint(x * 12, y + 80);
	pts[5] = cvPoint(x * 4, y + 80);
	cvFillConvexPoly(image, pts, 6, kolor2, 8, 0);
	cvInitFont(&czcionka, CV_FONT_HERSHEY_DUPLEX, 1.0f, 1.0f, 0, 2, 8);
	CvPoint point = cvPoint(width / 2 - 90, height / dzielnik);
	cvPutText(image, "New Game", point, &czcionka, kolor1);
	point.x = (width / 2 - 58);
	point.y = (height / dzielnik * 2);
	cvPutText(image, "Record", point, &czcionka, kolor1);
	point.x = (width / 2 - 110);
	point.y = (height / dzielnik * 3);
	cvPutText(image, "About Authors", point, &czcionka, kolor1);
	point.x = (width / 2 - 35);
	point.y = (height / dzielnik * 4);
	cvPutText(image, "Exit", point, &czcionka, kolor1);
	return 0;
}
int oAutorachDisplay(CvArr* image, Fizyka pilka, int width, int height) {

	CvFont czcionka;
	int dzielnik = 5;
	CvScalar kolor1 = CV_RGB(60,60,60);
	CvScalar kolor2 = CV_RGB(20,160,0);
	CvPoint pts[6];
	int x = width / 16;
	int y;
	CvPoint wyjscie = cvPoint(width / 2 - 50, height / dzielnik * 4);
	if (pilka.y > (height / dzielnik) * 4 - 55) {
		y = (height / dzielnik) * 4 - 50;
		pts[0] = cvPoint(x * 2, y + 40);
		pts[1] = cvPoint(x * 4, y);
		pts[2] = cvPoint(x * 12, y);
		pts[3] = cvPoint(x * 14, y + 40);
		pts[4] = cvPoint(x * 12, y + 80);
		pts[5] = cvPoint(x * 4, y + 80);
		cvFillConvexPoly(image, pts, 6, kolor2, 8, 0);
	}
	cvInitFont(&czcionka, CV_FONT_HERSHEY_DUPLEX, 1.0f, 1.0f, 0, 2, 8);
	CvPoint point = cvPoint(width / 2 - 80, height / dzielnik);
	cvPutText(image, "Created by:", point, &czcionka, kolor1);
	point.y = point.y + 40;
	cvPutText(image, "Artur Jaworski", point, &czcionka, kolor1);
	point.y = point.y + 40;
	cvPutText(image, "Mateusz Mor", point, &czcionka, kolor1);
	cvPutText(image, "Back", wyjscie, &czcionka, kolor1);
	return 0;
}
int rekordDisplay(CvArr* image, Fizyka pilka, int width, int height) {
	CvFont czcionka;
	int dzielnik = 5;
	int iloscPunktow = pilka.rekord;
	CvScalar kolor1 = CV_RGB(60,60,60);
	CvScalar kolor2 = CV_RGB(20,160,0);
	CvPoint pts[6];
	int x = width / 16;
	int y;
	CvPoint wyjscie = cvPoint(width / 2 - 50, height / dzielnik * 4);
	if (pilka.y > (height / dzielnik) * 4 - 55) {
		y = (height / dzielnik) * 4 - 50;
		pts[0] = cvPoint(x * 2, y + 40);
		pts[1] = cvPoint(x * 4, y);
		pts[2] = cvPoint(x * 12, y);
		pts[3] = cvPoint(x * 14, y + 40);
		pts[4] = cvPoint(x * 12, y + 80);
		pts[5] = cvPoint(x * 4, y + 80);
		cvFillConvexPoly(image, pts, 6, kolor2, 8, 0);
	}
	cvInitFont(&czcionka, CV_FONT_HERSHEY_DUPLEX, 1.0f, 1.0f, 0, 2, 8);
	CvPoint point = cvPoint(width / 10, height / dzielnik);
	char napis[100];
	strcpy(napis, "Your record for this turn: ");
	strcat(napis, intToStr(iloscPunktow));
	cvPutText(image, napis, point, &czcionka, kolor1);
	cvPutText(image, "Back", wyjscie, &czcionka, kolor1);
	return 0;
}
int viewDisplay(CvArr* image, Fizyka pilka, int width, int height) {
	CvFont czcionka;
	cvInitFont(&czcionka, CV_FONT_HERSHEY_DUPLEX, 1.0f, 1.0f, 0, 2, 8);
	CvScalar kolor1 = CV_RGB(60,60,60);
	if (pilka.mx > (width / 10) * 8 && pilka.my < 45)
		kolor1 = CV_RGB(20,160,0);
	CvPoint point = cvPoint((width / 10) * 8, 40);
	char napis[100];
	strcpy(napis, "Done");
	cvPutText(image, napis, point, &czcionka, kolor1);
	point.x = width / 2 - 120;
	point.y = height / 2 - 100;
	kolor1 = CV_RGB(60,60,60);

	if (pilka.dzielnik == 0) {
		cvPutText(image, "Difficulty level:", point, &czcionka, kolor1);
		point.y = height / 2 - 60;
		for (int i = 1; i < 6; i++) {
			point.x = width / 7 * i;
			if (pilka.counter == i)
				kolor1 = CV_RGB(20,160,0);
			else
				kolor1 = CV_RGB(60,60,60);
			cvPutText(image, intToStr(i), point, &czcionka, kolor1);
		}
	}

	return 0;
}
int displayscore(IplImage* image, Fizyka pilka) {
	CvFont czcionka;
	cvInitFont(&czcionka, CV_FONT_HERSHEY_DUPLEX, 1.0f, 1.0f, 0, 2, 8);
	CvScalar kolor1 = CV_RGB(160,60,0);
	char napis[100];
	strcpy(napis, "Game over. Your points: ");
	strcat(napis, intToStr(pilka.counter));
	CvPoint point = cvPoint((image->width / 2) - 180, image->height / 2);
	cvPutText(image, napis, point, &czcionka, kolor1);
	point.x = (image->width / 2) - 10;
	point.y = (image->height / 4) * 3 + 30;
	kolor1 = CV_RGB(60,60,60);
	//printf("pilsa score: %d\n",pilka.score);fflush(stdout);
	if (pilka.score == -2)
		kolor1 = CV_RGB(0,160,60);
	cvPutText(image, "OK", point, &czcionka, kolor1);
	return 0;
}
